/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.Serializable;

import org.jefb.service.IUserSessionService;
import org.jefb.web.model.DirectoryMananagerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Iframe;
import org.zkoss.zul.Label;
import org.zkoss.zul.Toolbar;

@org.springframework.stereotype.Component("pdfViewerController")
@Scope("prototype")
public class PdfViewerController extends GenericForwardComposer implements
		Serializable {
	
	private static final long serialVersionUID = 1L;

	// Models
	@Autowired
	private DirectoryMananagerModel directoryManagerModel;
	@Autowired
	private IUserSessionService userSessionService;
	
	protected Iframe pdfIframe;

	
	private File pdfFile;

	public PdfViewerController() {

	}

	public void init() {

	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		pdfFile=directoryManagerModel.getSelectedFile().getFile();
		createToolbar(comp);
		loadPdf(comp);
	}

	private void createToolbar(final Component comp) {
//		Toolbar imageToolbar = new Toolbar();
//		imageToolbar.setParent(comp);
//
//		Label scaleLabel = new Label("PDF:");
//		scaleLabel.setParent(imageToolbar);
	}

	/**
	 * Load image and fit it for given scale.
	 * @param comp
	 */
	private void loadPdf(Component comp) {
		try {
			//FileInputStream fis = new FileInputStream(pdfFile);
			AMedia pdfContainer = new AMedia(pdfFile.getName(), "pdf", null, pdfFile, true);
			pdfIframe.setContent(pdfContainer);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void setDirectoryManagerModel(
			DirectoryMananagerModel directoryManagerModel) {
		this.directoryManagerModel = directoryManagerModel;
	}

	public DirectoryMananagerModel getDirectoryManagerModel() {
		return directoryManagerModel;
	}

	public void setUserSession(IUserSessionService userSessionService) {
		this.userSessionService = userSessionService;
	}

	public IUserSessionService getUserSession() {
		return userSessionService;
	}

}
